#include <cstdlib>
#include <string.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>

using namespace std;

class Item{
public:
    Item(string &k,string &v):key(k),value(v){}

    string key;
    string value;
};

class ConfigContent{
public:

    void readConfig(string &filename){
        ifstream ifs(filename);
        
        if(!ifs.good()){
            cerr<<" -- Read Fail -- "<<endl;
            return ;
        }
        
        string line;
        string k;
        string v;

        while(getline(ifs,line)){
            istringstream iss(line);
            iss>>k>>v; 
            bool flag=1;
            for(size_t i=0;i<vec.size();i++){
                if(vec[i].key==k){
                    vec[i].value=v;
                    flag=0;
                    break;
                }
            }
            if(flag)
                vec.emplace_back(k,v);

    }
    }

    string get(string &word){
        for(auto it=vec.begin();it!=vec.end();it++){
            if((*it).key==word){
                return (*it).value;
            }
        }
        static string nullstr="\0";
        return nullstr;
    } 

    static ConfigContent* getInstance(){
        if(_p==nullptr){
            atexit(destroy);
            _p=new ConfigContent();
        }
        return _p;
    }

private:
    ConfigContent(){}
    ConfigContent(const ConfigContent &)=delete;
    ConfigContent& operator=(const ConfigContent &)=delete;
    ~ConfigContent(){}
    static void destroy(){
        if(_p){
            delete _p;
            _p=nullptr;    
         }
    }
    vector<Item> vec;
    static ConfigContent *_p;
};
ConfigContent* ConfigContent::_p;

void test(){
    ConfigContent *con=ConfigContent::getInstance();
    
    string readFile="./config.ini";
    con->readConfig(readFile);

    string ip="ip";
    cout<<" -- ip: "<< con->get(ip)<<" -- "<<endl;
}

int main()
{
 test();
 return 0;
}

